<?php
defined('IN_YuLin') || exit('NO PERMIT!');
/**
 * 设置彩种排序
 */
function setOrd($param){
    $res = array(
        'status' => 'success',
        'msg'    => '',
    );
    global $db;
    $lottTab = Table('lottery');
    foreach ($param as $k => $v){
        $sql = 'UPDATE ' . $lottTab . ' SET listorder = ' . $k . ' WHERE id = ' . $v;
        $db->exec($sql);
    }
    return $res;
}

/**
 * 设置信用彩种的开奖规则设置
 * @param array $param
 */
function setCreditLottOpen($param){
    $res   = array(
        'status' => 'success',
        'msg'    => '',
    );
    $table = Table('lottery_credit_play');
    if(!$param->lott_id) {
        $res['status'] = 'error';
        $res['msg']    = '彩种信息获取失败，请重试';
        return $res;
    }
    global $db;
    $sql        = 'SELECT count(id) as cnt FROM ' . $table . ' WHERE lott_id = ' . $param->lott_id;
    $checkExist = $db->getrow($sql);
    if($checkExist['cnt'] > 0){
        // 存在记录，则更新所有的状态为不使用。
        $sql = 'UPDATE ' . $table . ' SET status = 0 WHERE lott_id = ' . $param->lott_id;
        $db->exec($sql);
    }
    // 检查当前开奖方式是否有设计记录
    $sql        = 'SELECT id FROM ' . $table . ' WHERE lott_id = ' . $param->lott_id . ' AND type_id = ' . $param->type_id;
    $checkExist = $db->getrow($sql);
    if ($checkExist){
        // 存在记录， 执行修改操作
        $sql = 'UPDATE ' . $table . ' SET open_param = "' . $param->open_param . '", status = 1 WHERE id = ' . $checkExist['id'];
    }else{
        $ins = array(
            'lott_id' => $param->lott_id,
            'type_id' => $param->type_id,
            'open_param' => $param->open_param,
            'status'     => 1,
        );
        // 新增记录
        $sql = 'INSERT INTO ' . $table . ' SET ' . CreateUpdateSql($ins);
    }
    $flag = $db->exec($sql);
    if (!$flag){
        $res['status'] = 'error';
        $res['msg']    = '更新失败，请重试';
    }
    return $res;
}

/**
 * 获取彩种设置的开奖方式信息（针对信用彩种）
 */
function getSetCreditInfo($lott_id){
    global $db;
    $sql        = 'SELECT id, type_id, open_param, status FROM ' . Table('lottery_credit_play') . ' WHERE lott_id = ' . $lott_id;
    $creditData = $db->getall($sql);
    if (!$creditData){
        return;
    }
    $creditTypeIdKey = array();
    foreach ($creditData as $k => $v) {
        $creditTypeIdKey[$v['type_id']] = $v;
    }
    return array('creditSet' => $creditTypeIdKey);
}

/**
 * 根据指定的彩种类别，查询返点赔率设置
 */
function getRepointOddsList($cateid, $point=''){
    global $db;
    $cateTab   = Table('lottery_category');
    $mainTab   = Table('lottery_play_main');
    $follTab   = Table('lottery_play_follow');
    $raosTab   = Table('repoint_and_odds');
    $typeTab   = Table('lottery_play_type');
    $moreStatus= false;
    // 如果没有设定point的节点，则默认载入整数节点
    $ord       = ' GROUP BY re_point ORDER BY re_point DESC';
    if (!$point){
        $sql       = 'SELECT id, re_point FROM ' . $raosTab . ' WHERE lott_cate_id = ' . $cateid . ' AND is_node = 1' . $ord;
        $rePointTitle  = $db->getall($sql);
    }else{
        $sql       = 'SELECT id, re_point, is_node FROM ' . $raosTab . ' WHERE lott_cate_id = ' . $cateid  . $ord;
        $allRepoint    = $db->getall($sql);
        
        foreach ($allRepoint as $k => $v){
            if ($v['re_point'] == $point){
                $v['is_node']   = 0;
            }
            if ($v['re_point'] <= $point && !$v['is_node']){
                $rePointTitle[] = $v;
            }
            if($v['re_point'] < $point && $v['is_node']) break;
        }
        $moreStatus = true;
    }
    
    // 查询获取的返点下的赔率信息
    $repointStr  = ' (-1';
    foreach ($rePointTitle as $k => $v){
        $repointStr .= ', ' . $v['re_point'];
    }
    $repointStr .= ' )';
    $sql = 'SELECT repoint.`id`, repoint.`re_point`, repoint.`odds`, main.`title` as main_title, foll.`title` as foll_title, repoint.`follow_id` 
            FROM ' . $raosTab . ' AS repoint
            LEFT JOIN ' . $cateTab . ' AS cate 
            ON repoint.`lott_cate_id` = cate.`id`
            LEFT JOIN ' . $follTab . ' AS foll
            ON repoint.`follow_id` = foll.`id`
            LEFT JOIN ' . $mainTab . ' AS main
            ON foll.`main_id` = main.`id`
            WHERE repoint.`lott_cate_id` = ' . $cateid . ' AND repoint.`re_point` IN ' . $repointStr . '
            ORDER BY repoint.`re_point` DESC';
    
    $list = $db->getall($sql);
    $followIdKeyData = array();
    foreach ($list as $k => $v){
        $followIdKeyData[$v['main_title'] . ' - ' . $v['foll_title']][] = $v;
    }
    return array('rpTitle' => $rePointTitle, 'list' => $followIdKeyData, 'moreSta' => $moreStatus);
}

/**
 * 修改返点赔率关系
 */
function setRepointOdds($id, $odds){
    global $db;
    $res   = array(
        'status' => 'success',
        'msg'    => '',
    );
    if (!$id || !$odds){
        $res['status'] = 'error';
        $res['msg']    = '提交信息缺失';
    }
    $sql  = 'UPDATE ' . Table('repoint_and_odds') . ' SET odds = ' . $odds . ' WHERE id = ' . $id;
    $flag = $db->exec($sql);
    if (!$flag){
        $res['status'] = 'error';
        $res['msg']    = '设置失败';
    }
    return $res;
}
















